<!-- 水源热泵能耗 -->
<template>
  <div class="chart-list" v-loading="loading">
    <div class="chart-box" v-for="item in typeList" :key="item.dictCode">
      <Line :loading="item.loading" :chartData="item.chartData" :title="item.itemName" />
    </div>
  </div>
</template>

<script setup>
import Line from '@/components/Charts/Line/index.vue'
import { ref, onMounted, watch, computed } from 'vue'
import { getChartChildListData } from '@/api/common/chart'
import { getDictListByCode } from '@/api/sys/dict'

defineOptions({ name: 'WaterSourceHeatPump' })

const props = defineProps({
  searchInfo: {
    type: Object,
    default: () => ({}),
  },
  label: {
    type: String,
    default: '',
  },
})

const typeList = ref([])
const chartList = ref([])
const dataReady = ref(false)
const dictCode = 'energy_consumption_of_water_source_heat_pump'

const loading = computed(() => {
  return !dataReady.value
})

onMounted(async () => {
  await getTypeList()
})

const getTypeList = async () => {
  const res = await getDictListByCode(dictCode)
  dataReady.value = true
  typeList.value = res.map((item) => ({
    ...item,
    loading: true,
    chartData: {
      xaxisList: [],
      yaxisList: [],
    },
  }))
}
const handleSearch = async (params) => {
  if (dataReady.value) {
    getChartData(params)
  } else {
    setTimeout(() => {
      handleSearch(params)
    }, 200)
  }
}
watch(
  () => props.searchInfo,
  (newValue) => {
    if (Object.values(newValue).length > 0) {
      handleSearch(newValue)
    }
  },
  { immediate: true },
)
const getChartData = (searchInfo) => {
  typeList.value.forEach((item) => {
    item.loading = true
    getChartChildListData({
      ...searchInfo,
      dictCode,
      itemName: item.itemName,
    })
      .then((res) => {
        item.chartData = res
      })
      .finally(() => {
        item.loading = false
      })
  })
}
</script>

<style scoped lang="scss">
.chart-list {
  display: flex;
  flex-wrap: wrap;
  height: 100%;

  .chart-box {
    flex-shrink: 0;
    width: 50%;
    height: 50%;
    padding: 4px;
  }
}
</style>
